﻿using System;
using System.Web;
namespace Common.Web
{
    /// <summary>
    /// 操作Cookie
    /// </summary>
    public class Cookie
    {

        /// <summary>
        /// 创建Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <param name="strValue">Cookie 键值</param>
        /// <param name="strDay">Cookie 天数(单位：30分钟)</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.setCookie("主键","键值","天数");</code>
        public bool SetCookie(string strName, string strValue, int strDay)
        {
            try
            {
                //DelCookie(strName);
                string port = HttpContext.Current.Request.Url.Port.ToString();
                strName = strName + ":" + port;
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(strDay / 48.0);
                //  Cookie.Expires = DateTime.Now.AddDays(strDay / 1.0);
                Cookie.Value = strValue;
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                //     Common.LogManager.GetInstance().Log("设置COOKIE失败");
                return false;
            }
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.getCookie("主键");</code>
        public string GetCookie(string strName)
        {
            string port = HttpContext.Current.Request.Url.Port.ToString();
            strName = strName + ":" + port;
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                if (Cookie.Value != null)
                {
                    return Cookie.Value.ToString();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.delCookie("主键");</code>
        public bool DelCookie(string strName)
        {
            try
            {
                string port = HttpContext.Current.Request.Url.Port.ToString();
                strName = strName + ":" + port;
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(-100);
                Cookie.Value = null;
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 得到网站的Cookie
        /// </summary>
        /// <param name="url">网站域名 如http://www.xxx.com</param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public System.Net.CookieCollection GetCookieCollection(string url, System.Net.CookieContainer cookie)
        {
            Uri uri = new Uri(url);
            System.Net.CookieCollection ccll = cookie.GetCookies(uri);
            return ccll;
        }

    }
}
